/**
 * made in 2007-12-2上午06:43:39
 * 唉 你丫真二 
 */
package com.litxuf.web.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import net.litxuf.Util.LoggerHelper;

import org.apache.log4j.Logger;

/**
 * @author xufeng
 * 
 */
public class SitePaginationTag extends TagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	Logger logger = Logger.getLogger(this.getClass().getName());

	String url = "";
	int currPage;
	int totalPage;
	int totalRowCount;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return the currPage
	 */
	public int getCurrPage() {
		return currPage;
	}

	/**
	 * @param currPage
	 *            the currPage to set
	 */
	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	/**
	 * @return the totalPage
	 */
	public int getTotalPage() {
		return totalPage;
	}

	/**
	 * @param totalPage
	 *            the totalPage to set
	 */
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	/**
	 * @return the totalRowCount
	 */
	public int getTotalRowCount() {
		return totalRowCount;
	}

	/**
	 * @param totalRowCount
	 *            the totalRowCount to set
	 */
	public void setTotalRowCount(int totalRowCount) {
		this.totalRowCount = totalRowCount;
	}

	public int doEndTag() throws JspException {
		return EVAL_BODY_INCLUDE;
	}

	public int doStartTag() throws JspException {

		try {
			JspWriter out = pageContext.getOut();

			StringBuffer strHtml = new StringBuffer();
			;
			int pageCount = totalPage;
			int page = currPage;
			int prevPage = page - 1, nextPage = page + 1;
			int endPage = 0;

			strHtml.append("<span class=current >页码: " + page + " / "
					+ pageCount + " 总记录:" + totalRowCount + "</span>");
			strHtml.append("<span class='number'>");
			if (prevPage < 1) {

			} else {

				strHtml.append("<a href='" + url + "/" + prevPage
						+ ".html'>上一页</a>");
			}

			if (pageCount > page + 3) {
				endPage = page + 3;
			} else {
				endPage = pageCount;
			}
			for (int i = page - 3; i <= endPage; i++) {
				if (i > 0) {
					if (i == page) {
						strHtml.append("<SPAN class=current>" + i + "</span>");
					} else {

						strHtml.append("<a href='" + url + "/" + i + ".html'>"
								+ i + "</a>");

					}
				}
			}

			if (nextPage > pageCount) {

			} else {
				strHtml.append("<a href='" + url + "/" + nextPage
						+ ".html'>下一页</a>");

			}
			strHtml.append("</span><br />");

			logger.info(":::---> " + strHtml.toString());

			out.print(strHtml.toString());

		} catch (Exception e) {

			LoggerHelper.error(logger, e);
		}

		return EVAL_PAGE;
	}

	public static void main(String[] args) {
		int i = 4;
	}

}
